package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.TReport;
import com.ruoyi.system.domain.vo.TReportVo;
import com.ruoyi.system.mapper.TReportMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.TAuditBo;
import com.ruoyi.system.domain.vo.TAuditVo;
import com.ruoyi.system.domain.TAudit;
import com.ruoyi.system.mapper.TAuditMapper;
import com.ruoyi.system.service.ITAuditService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 审核表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-11
 */
@RequiredArgsConstructor
@Service
public class TAuditServiceImpl implements ITAuditService {

    private final TAuditMapper baseMapper;
    private final TReportMapper tReportMapper;

    /**
     * 查询审核表
     */
    @Override
    public TAuditVo queryById(String pkid){
        TAuditVo vo = baseMapper.selectVoById(pkid);
        TReportVo status = tReportMapper.selectVoById(vo.getTrId());
        vo.setAuditStatus(status.getAudit());
        return vo;
    }

    /**
     * 查询审核表列表
     */
    @Override
    public TableDataInfo<TAuditVo> queryPageList(TAuditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TAudit> lqw = buildQueryWrapper(bo);
        Page<TAuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询审核表列表
     */
    @Override
    public List<TAuditVo> queryList(TAuditBo bo) {
        LambdaQueryWrapper<TAudit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TAudit> buildQueryWrapper(TAuditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TAudit> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTrId()), TAudit::getTrId, bo.getTrId());
        return lqw;
    }

    /**
     * 新增审核表
     */
    @Override
    public Boolean insertByBo(TAuditBo bo) {
        TAudit add = BeanUtil.toBean(bo, TAudit.class);
        validEntityBeforeSave(add);
        LambdaQueryWrapper<TAudit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TAudit::getTrId,bo.getTrId());
        List<TAuditVo> vos = baseMapper.selectVoList(wrapper);
        if (vos.isEmpty()) {
            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setPkid(add.getPkid());
            }
        }
        LambdaUpdateWrapper<TReport> update = new LambdaUpdateWrapper<>();
        update.set(TReport::getAudit,bo.getAuditStatus())
                                .eq(TReport::getPkid,bo.getTrId());
        return tReportMapper.update(update) > 0;
    }

    /**
     * 修改审核表
     */
    @Override
    public Boolean updateByBo(TAuditBo bo) {
        TAudit update = BeanUtil.toBean(bo, TAudit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TAudit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除审核表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
